package leaderus.study;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;

public class MyPersonCaclByArrayTable {
	public static void main(String[] args) {
		MyPersonCaclByArrayTable mp=new MyPersonCaclByArrayTable();
		String tableFile=GenRandomersonDBFile.path + "//"+ GenRandomersonDBFile.fileName;
		Table<String, String, String> table = mp.readTableFile(tableFile);
		long startTime=System.currentTimeMillis();
		Integer age = 0;
		String name = "";
		int sixToEleven = 0;
		int sixTeeToEigTee = 0;
		int a = 0, b = 0, c = 0, d = 0, e = 0;
		int a1 = 0, b1 = 0, c1 = 0, d1 = 0, e1 = 0;
		List<String> resultRowList = Lists.newArrayList("6-11", "16-18");
		List<String> resultColumnList = Lists.newArrayList("total", "a", "b","c", "d", "e");
		Table<String, String, Integer> resultTable = ArrayTable.create(resultRowList, resultColumnList);
		
		Map<String,Map<String,String>> personInfoMap=table.rowMap();
		Collection<Map<String, String>> values = personInfoMap.values();
		Iterator<Map<String, String>> iterator = values.iterator();
		Map<String,String> person=new HashMap();
		char first;
		while(iterator.hasNext()){
			person=iterator.next();
			age=Integer.valueOf(person.get("age"));
			name=person.get("name");
					
			first=name.charAt(0);
			if (age > 5 && age < 12) {
				sixToEleven++;
				if (first=='a' || first=='A')
					a++;
				if (first=='b' || first=='B')
					b++;
				if (first=='c' || first=='C')
					c++;
				if (first=='d' || first=='D')
					d++;
				if (first=='e' || first=='E')
					e++;
			}
			if (age > 15 && age < 19) {
				sixTeeToEigTee++;
				if (first=='a' || first=='A')
					a1++;
				if (first=='b' || first=='B')
					b1++;
				if (first=='c' || first=='C')
					c1++;
				if (first=='d' || first=='D')
					d1++;
				if (first=='e' || first=='E')
					e1++;
			}
		}
		System.out.println("查询出统计结果共耗时："+(System.currentTimeMillis()-startTime)+" 毫秒。");
		resultTable.put("6-11", "total", sixToEleven);
		resultTable.put("6-11", "a", a);
		resultTable.put("6-11", "b", b);
		resultTable.put("6-11", "c", c);
		resultTable.put("6-11", "d", d);
		resultTable.put("6-11", "e", e);

		resultTable.put("16-18", "total", sixTeeToEigTee);
		resultTable.put("16-18", "a", a1);
		resultTable.put("16-18", "b", b1);
		resultTable.put("16-18", "c", c1);
		resultTable.put("16-18", "d", d1);
		resultTable.put("16-18", "e", e1);

		System.out.println("6-11岁总共：" + resultTable.get("6-11", "total")
				+ " 个，a开头的：" + resultTable.get("6-11", "a") + " 个，b开头的："
				+ resultTable.get("6-11", "b") + " 个，c开头的："
				+ resultTable.get("6-11", "c") + " 个，d开头的："
				+ resultTable.get("6-11", "d") + " 个，e开头的："
				+ resultTable.get("6-11", "e"));
		System.out.println("16-18岁总共：" + resultTable.get("16-18", "total")
				+ " 个，a开头的：" + resultTable.get("16-18", "a") + " 个，b开头的："
				+ resultTable.get("16-18", "b") + " 个，c开头的："
				+ resultTable.get("16-18", "c") + " 个，d开头的："
				+ resultTable.get("16-18", "d") + " 个，e开头的："
				+ resultTable.get("16-18", "e"));
	}
	
	public Table readTableFile(String fileName) {
		File personFile = new File(fileName);
		Table<String, String, String> table=null;
		if (personFile.exists() && personFile.isFile()) {
			try {
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(personFile));
				BufferedReader bufferedReader = new BufferedReader(read);
				String infoLine;
				String[] infos;
				List<String> rowList =Lists.newArrayList();
				List<String> columnList = Lists.newArrayList("name", "age");
				String[] ageArray=new String[1000000];
				String[] nameArray=new String[1000000];
				int i=0;
				while ((infoLine = bufferedReader.readLine()) != null) {
					infos = infoLine.split(",");
					rowList.add(infos[0]);
					nameArray[i]=infos[1];
					ageArray[i]=infos[2];
					i++;
				}
				table = ArrayTable.create(rowList, columnList);
				for(int j=0;j<1000000;j++){
					table.put(rowList.get(j), columnList.get(0),nameArray[j]);
					table.put(rowList.get(j), columnList.get(1),ageArray[j]);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("找不到指定文件！");
		}
		return table;
	}
}
